package com.itheima.demo02;

public class AlgorithmTest02 {
    public static void main(String[] args) {
        //二分查找法
        int[] arr = {2, 33, 44, 56, 78};
        int num = 33;
        System.out.println(queryIndex(arr, num));

    }

    private static int queryIndex(int[] arr, int num) {
        //1.定义两个变量
        int min = 0;
        int max = arr.length - 1;

        while (true) {
            if (min > max) {
                return -1;
            }
            int mid = (min + max) / 2;

            if (arr[mid] > num) {
                max = mid - 1;
            } else if (arr[mid] < num) {
                min = mid + 1;
            } else {
                return mid;
            }
        }
    }
}
